package com.my.cd.memo;

import java.util.HashMap;
import java.util.Map;

/**
 * 进度或状态存储发起者。即当前程序的运行环境类。
 */
public class Originator {

    private Map<String, String> state;

    public Originator() {
        state = new HashMap<>();
    }

    /**
     * 创建备忘录
     */
    public Memento createMemento() {
        return new MementoA(state);
    }

    /**
     * 恢复备忘录
     */
    public void restoreMemento(Memento memento) {
        state = ((MementoA) memento).getState();
    }

    public void showState() {
        System.out.println("now state:" + state.toString());
    }

    public void testState1() {
        state.put("blood", "500");
        state.put("progress", "gate1 end");
        state.put("enemy", "5");

    }

    public void testState2() {
        state.put("blood", "450");
        state.put("progress", "gate3 start");
        state.put("enemy", "3");

    }

    private class MementoA implements Memento {

        private Map<String, String> state;

        private MementoA(Map<String, String> state) {
            this.state = new HashMap<>(state);
        }

        public Map<String, String> getState() {
            return state;
        }

        public void setState(Map<String, String> state) {
            this.state = state;
        }
    }
}
